% clear;clc;
% a = [6,2,1;2.25,1,0.2;3,0.2,1.8];
% b = [25,5,20];
% c = a/(diag(b));            % 生产一个单位需要消耗对应量的其他物资
% A = eye(3,3) - c;
% D = [17;17;17];
% % ret = D\A;
% ret = A\D;
% e = ret* D;
% clear; clc;
% city = 0.7;
% restic = 0.3;
% 
% map = zeros(10000,2);
% sum = 0;
% for i = (1:10000)
%     city = city * 0.99 +restic *0.05;
%     restic = city * 0.01 + restic*0.95;
%     sum = city + restic;
%     city = city/sum;
%     restic = restic / sum;
%     map(i,1) = city;
%     map(i,2) = restic;
% end


% ret = fzero(@fcode,5);
% % tmp1 = (-10:0.05:10);
% % tmp2 = zeros(length(tmp1));
% % for i = (1:length(tmp1))
% %     tmp2(i) = fcode(tmp1(i));
% % end
% %plot(tmp1,tmp2);
% fplot(@fcode,[-10,10]);
% % 经检验符合
% 
% fun = inline( ...
%     [ ...
%     '[9*x(1)^2+36*x(2)^2+4*x(3)^2-36;' ...
%     'x(1)^2-2*x(2)^2-20*x(3);' ...
%     '16*x(1)-x(1)^3-2*x(2)^2-16*x(3)^2]'] ...
%     ,'x');
% ff = inline( ...
%     [ ...
%     '[9*x^2+36*y^2+4*z^2-36;' ...
%      'x^2-2*y^2-20*z;' ...
%      '16*x-x^3-2*y^2-16*z^2]' ...
%      ] ...
%     ,'x,y,z'...
%     );
% 
% [a1,b1,c1] = fsolve(@ff,[0,0,0]);



% % 使用 inline 创建多元方程组
% ff = inline( ...
%     [ ...
%     '[9*x.^2+36*y.^2+4*z.^2-36;' ...
%      'x.^2-2*y.^2-20*z;' ...
%      '16*x-x.^3-2*y.^2-16*z.^2]'] ...
%      ,'x','y,'z' ...
% );
% 
% % 由于 fsolve 需要一个列向量，我们直接将初始猜测值作为列向量
% x0 = [0, 0, 0];
% 
% % 调用 fsolve 来求解方程组
% options = optimoptions('fsolve', 'Display', 'iter');  % 显示迭代过程
% [a1, b1, c1] = deal(fsolve(@(x) ff(x(1), x(2), x(3)), x0, options));




% clear;clc;
% ff = inline( ...
%     [ '[9*x.^2+36*y.^2+4*z.^2-36;' ...
%       'x.^2-2*y.^2-20*z; ' ...
%       '16*x-x.^3-2*y.^2-16*z.^2]'], ...
%     'x', 'y', 'z' ...
% );
% [a,b,c] = fsolve(@(x)ff(x(1),x(2),x(3)),[0,0,0]);
% 由于 fsolve 需要一个列向量，我们直接将初始猜测值作为列向量
% x0 = [0, 0, 0];
% 
% % 调用 fsolve 来求解方程组
% options = optimoptions('fsolve', 'Display', 'iter');  % 显示迭代过程
% [a1, b1, c1] = deal(fsolve(@(x) ff(x(1), x(2), x(3)), x0, options));
% 
% % 输出结果
% disp(['解得 x = ', num2str(a1)]);
% disp(['解得 y = ', num2str(b1)]);
% disp(['解得 z = ', num2str(c1)]);

fun = @(x)fcode(x);
fplot(fun,[-2,2]);

min1 = fminbnd(fun,-2,-1.5);
min2 = fminbnd(fun,-1.5,0);
min3 = fminbnd(fun,0,2);
% f_to_max = @(x)fcode2(x);
% 
% max1 = fminbnd( f_to_max,-2,2);


f_to_min = @(x) fcode2(x);

% 调用 fminbnd 寻找 fcode 的极大值
max1 = fminbnd(f_to_min, -2, 2);